import 'package:realm/realm.dart';

part 'model.realm.dart';

///信号模组
@RealmModel()
class _SignModel {
  late int freq;
  late String name;
  late int index;
  double gain = 0;
  double Q = 7.2;
  double min_gain = -20;
  double max_gain = 20;
  double min_Q = 0.5;
  double max_Q = 20;
  bool select = false;
  bool enable = false;
}

///通道
@RealmModel()
class _ChannelModel {
  @PrimaryKey()
  int num = 0;
  String name = '';
  bool mute = false;
  bool phase = false;
  int db = 0;
  int max_db = 0;
  int min_db = -50;
  bool select = false;
  bool enable = false;
  int hex_line = 0xFFFFFFFF;
  String delay = '0';
  double min_delay = 0;
  double max_delay = 8;
  String delay_type = 'ms';
  List<int> input_list = [];
  List<int> input_assign_list = [];
  List<int> input_custom_list = [];
  late _Passmodel? high_pass;
  late _Passmodel? low_pass;
  List<_SignModel> sign_list = [];
}

///滤波
@RealmModel()
class _Passmodel {
  late String name;
  late bool ishigh;
  late int freq;
  int min_freq = 20;
  int max_freq = 20000;
  bool select = false;
  bool enable = false;
  bool isBypass = false;
  late _PassType? type;
}

///滤波类型
@RealmModel()
class _PassType {
  late String name;
  late int slope;
  late final List<int> slopeList;
  int get oct_type {
    switch (name) {
      case 'Link-R':
        switch (slope) {
          case 6:
            return 0;
          case 12:
            return 1;
          case 18:
            return 2;
          case 24:
            return 3;
          case 36:
            return 4;
          case 48:
            return 5;
          default:
            return -1;
        }
      case 'Bessel':
        switch (slope) {
          case 6:
            return 0;
          case 12:
            return 11;
          case 18:
            return 12;
          case 24:
            return 13;
          case 36:
            return 14;
          case 48:
            return 10;
          default:
            return -1;
        }
      case 'Butter-W':
        switch (slope) {
          case 6:
            return 0;
          case 12:
            return 6;
          case 18:
            return 7;
          case 24:
            return 8;
          case 36:
            return 9;
          case 48:
            return 10;
          default:
            return -1;
        }
      default:
        return -1;
    }
  }

  ///蓝牙发送数据的参数，滤波类型
  int get type {
    switch (name) {
      case 'Link-R':
        return 2;
      case 'Bessel':
        return 0;
      case 'Butter-W':
        return 1;
      default:
        return 0;
    }
  }

  ///蓝牙发送数据的类型，滤波斜率
  int get slope_hex {
    switch (slope) {
      case 6:
        return 0;
      case 12:
        return 1;
      case 24:
        return 2;
      case 36:
        return 3;
      case 48:
        return 4;
      default:
        return 0;
    }
  }
}

///通道组
@RealmModel()
class _GroupModel {
  late _ChannelModel? ch1;
  late _ChannelModel? ch2;
  late _ChannelModel? target_ch;
  String name = '';
  bool _select = false;
  bool enable = false;
  bool get select => _select;
  set select(bool isseclt) {
    _select = isseclt;

    ch1?.select = isseclt;
    ch2?.select = isseclt;
  }

  bool contain(_ChannelModel ch) {
    return ch1!.name == ch.name || ch2!.name == ch.name;
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    if (other is! _GroupModel) return false;

    return other.name == name;
  }
}

///全局管理类
@RealmModel()
class _GlobalModel {
  @PrimaryKey()
  late String tag;
  bool mute = false;
  late int db = 0;
  late int max_db = 0;
  late int min_db = -50;
  bool enable = false;
  bool isLock = false;
  String date = '';
  String input_type = '';
  String copy_stype = '';
  late List<_ChannelModel> ch_list = [];
  String data = '';
}

///分类 私有类转 realm模型
extension _SignModelExtension on _SignModel {
  SignModel get toModel {
    return SignModel(freq, name, index,
        gain: gain,
        Q: Q,
        min_gain: min_gain,
        max_gain: max_gain,
        min_Q: min_Q,
        max_Q: max_Q,
        select: select,
        enable: enable);
  }
}

extension _PassModelExtension on _Passmodel {
  Passmodel get toModel {
    return Passmodel(name, ishigh, freq,
        min_freq: min_freq, max_freq: max_freq, select: select, enable: enable, type: type as PassType);
  }
}

extension _ChannelModelExtension on _ChannelModel {
  ChannelModel get toModel {
    return ChannelModel(this.num,
        name: name,
        mute: mute,
        phase: phase,
        db: db,
        max_db: max_db,
        min_db: min_db,
        select: select,
        enable: enable,
        hex_line: hex_line,
        delay: delay,
        input_list: input_list,
        high_pass: high_pass?.toModel,
        low_pass: low_pass?.toModel,
        sign_list: sign_list.map(($0) => $0.toModel).toList());
  }
}
